package com.hqyj.springBoot.filter;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebFilter(filterName = "ParamFilter",urlPatterns = "/*")//定义匹配的规则
public class ParamFilter implements Filter {

    private static final Logger Logger = LoggerFactory.getLogger(ParamFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //初始化过滤器
        Logger.debug("========== ParamFilter");
    }

    @Override//这里重写的是通过request.getParameter这个方式获取的参数
    //在doFilter中要把处理过的请求和响应的内容发送出去
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        Logger.debug("========== DoFilter");
        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;//取值的话用的是httpServletRequest所以这里要把servletRequest转换为它
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
//        Map<String,String[]> map = httpServletRequest.getParameterMap();
//        map.put("123",new String[]{"123"}); 这里的map是经过锁住的所以不能进行增删改（也就是修改的意思）
        HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(httpServletRequest){
            @Override
            public String getParameter(String name) {
                String value = httpServletRequest.getParameter(name);
                if(StringUtils.isNotBlank(value)){//判断前端传来的数据是否为空
                    return value.replaceAll("fuck","****");
                }
                return super.getParameter(name);
            }

            @Override//这里重写的是通过注解获取参数的方法
            public String[] getParameterValues(String name) {
                String[] values = httpServletRequest.getParameterValues(name);
                if (values != null && values.length > 0) {
//                    List<String> temp = new ArrayList<>();//因为直接改元素返回的结果没变化所以这里的逻辑是将，改变后的元素重新装入一个新的数组中
//                    for (String value : values) {
//                        temp.add(value.replaceAll("fuck", "***"));
//                    }
//                    return temp.toArray(new String[temp.size()]);//将集合转变为数组并给出长度

                    //流式表达式转换方法
                    return Arrays.asList(values).stream()//将string数组转变成集合并将它变为stream对象
                            .map(item -> item.replaceAll("fuck", "****"))//包装（定义了一个临时变量item将每一项中的fuck变为****）
                            .toArray(String[] :: new);//因为走到这里的时候他还是一个stream对象所以要再次转换为数组
                }

                return super.getParameterValues(name);
            }
        };

        filterChain.doFilter(requestWrapper,httpServletResponse);//这里返回的数据是已经经过过滤处理的
    }

    @Override
    public void destroy() {
        Logger.debug("========== DestroyFilter");
    }
}
